clear*

use "${root}/data/processed/final_sample.dta", clear

keep if baseline_sample == 1 & oil_sample == 1

********* Perform (robust bias-corrected) RD estimates and produce results table **********

foreach outcome in tot_exp_avg_pc tot_exp_avg_sgdp tot_rev_avg_pc tot_rev_avg_sgdp current_exp_avg_share investments_avg_share personnel_avg_share social_exp_avg_share social_exp_avg_pc health_avg_share education_avg_share welfare_avg_share nonsocial_exp_avg_share housing_avg_share transport_avg_share other_avg_share {

*** (1) baseline (whole sample, average over 4-years, CCT 2014 MSE-optimal bandwidth)

	qui rdrobust res_`outcome' margin_mayor_left , vce(cluster mun_code) all

	local coeff1_`outcome' : di %9.2f (e(tau_bc))
	local se1_`outcome' : di %9.2f (e(se_tau_rb))
	local pv1_`outcome' : di %9.2f (e(pv_rb))
	local n1_`outcome' = e(N)
	local effn1_`outcome' = e(N_h_l) + e(N_h_r)


*** (2) 1st year
	local res_outcome_1st = regexr("res_`outcome'", "_avg", "_1st")
	dis as text "variable now is `res_outcome_1st'"

	qui rdrobust `res_outcome_1st' margin_mayor_left , vce(cluster mun_code) all

	local coeff2_`outcome' : di %9.2f (e(tau_bc))
	local se2_`outcome' : di %9.2f (e(se_tau_rb))
	local pv2_`outcome' : di %9.2f (e(pv_rb))
	local n2_`outcome' = e(N)
	local effn2_`outcome' = e(N_h_l) + e(N_h_r)

*** (3) 2nd year
	local res_outcome_2nd = regexr("res_`outcome'", "_avg", "_2nd")
	dis as text "variable now is `res_outcome_2nd'"

	qui rdrobust `res_outcome_2nd' margin_mayor_left , vce(cluster mun_code) all

	local coeff3_`outcome' : di %9.2f (e(tau_bc))
	local se3_`outcome' : di %9.2f (e(se_tau_rb))
	local pv3_`outcome' : di %9.2f (e(pv_rb))
	local n3_`outcome' = e(N)
	local effn3_`outcome' = e(N_h_l) + e(N_h_r)

*** (4) 3rd year
	local res_outcome_3rd = regexr("res_`outcome'", "_avg", "_3rd")
	dis as text "variable now is `res_outcome_3rd'"

	qui rdrobust `res_outcome_3rd' margin_mayor_left , vce(cluster mun_code) all

	local coeff4_`outcome' : di %9.2f (e(tau_bc))
	local se4_`outcome' : di %9.2f (e(se_tau_rb))
	local pv4_`outcome' : di %9.2f (e(pv_rb))
	local n4_`outcome' = e(N)
	local effn4_`outcome' = e(N_h_l) + e(N_h_r)

*** (5) 4th year
	local res_outcome_4th = regexr("res_`outcome'", "_avg", "_4th")
	dis as text "variable now is `res_outcome_4th'"

	qui rdrobust `res_outcome_4th' margin_mayor_left , vce(cluster mun_code) all

	local coeff5_`outcome' : di %9.2f (e(tau_bc))
	local se5_`outcome' : di %9.2f (e(se_tau_rb))
	local pv5_`outcome' : di %9.2f (e(pv_rb))
	local n5_`outcome' = e(N)
	local effn5_`outcome' = e(N_h_l) + e(N_h_r)

*** (8) previous term average
		local res_outcome_lag = regexr("res_`outcome'", "_avg", "_lag_avg")
		dis as text "variable now is `res_outcome_lag'"

		qui rdrobust `res_outcome_lag' margin_mayor_left if placebo_lagged_sample == 1, vce(cluster mun_code) all

		local coeff8_`outcome' : di %9.2f (e(tau_bc))
		local se8_`outcome' : di %9.2f (e(se_tau_rb))
		local pv8_`outcome' : di %9.2f (e(pv_rb))
		local n8_`outcome' = e(N)
		local effn8_`outcome' = e(N_h_l) + e(N_h_r)

}


* write table
texdoc init "${root}/results/tables/dynamic_oil.tex", replace force


tex \caption{Dynamic effects and pre-trends in the oil windfall subsample}
tex \resizebox*{1\linewidth}{!}{
tex \begin{tabular}{lcccccc}
tex \toprule
tex \multirow{2}{*}{Outcome}			&        Previous Mandate 					& Overall Mandate								& \multicolumn{4}{c}{Dynamics} \\
tex 						\cmidrule(lr){2-2}				 		\cmidrule(lr){3-3} 						\cmidrule(lr){4-7}
tex  						&        Average   						& Average	&      1st year   								&       2nd year 								&    3rd year   								&    4th year 					  			\\
tex \midrule
tex \multicolumn{7}{c}{Size of government: overall revenues and expenses} \\
tex \midrule
tex Expenditure per capita 			& `coeff8_tot_exp_avg_pc'`stars8_tot_exp_avg_pc' 										& `coeff1_tot_exp_avg_pc'`stars1_tot_exp_avg_pc' 											& `coeff2_tot_exp_avg_pc'`stars2_tot_exp_avg_pc' 											& `coeff3_tot_exp_avg_pc'`stars3_tot_exp_avg_pc' 											& `coeff4_tot_exp_avg_pc'`stars4_tot_exp_avg_pc' 											& `coeff5_tot_exp_avg_pc'`stars5_tot_exp_avg_pc' 			\\
tex  														& (`se8_tot_exp_avg_pc') 																						& (`se1_tot_exp_avg_pc')																							& (`se2_tot_exp_avg_pc') 																							& (`se3_tot_exp_avg_pc') 																							& (`se4_tot_exp_avg_pc') 																							& (`se5_tot_exp_avg_pc') 						\\
tex Expenditure, \% of GDP 			& `coeff8_tot_exp_avg_sgdp'`stars8_tot_exp_avg_sgdp' 								& `coeff1_tot_exp_avg_sgdp'`stars1_tot_exp_avg_sgdp'									& `coeff2_tot_exp_avg_sgdp'`stars2_tot_exp_avg_sgdp' 									& `coeff3_tot_exp_avg_sgdp'`stars3_tot_exp_avg_sgdp' 									& `coeff4_tot_exp_avg_sgdp'`stars4_tot_exp_avg_sgdp' 									& `coeff5_tot_exp_avg_sgdp'`stars5_tot_exp_avg_sgdp' 		 	\\
tex  														& (`se8_tot_exp_avg_sgdp') 																					& (`se1_tot_exp_avg_sgdp')																						& (`se2_tot_exp_avg_sgdp') 																						& (`se3_tot_exp_avg_sgdp') 																						& (`se4_tot_exp_avg_sgdp') 																						& (`se5_tot_exp_avg_sgdp') 						\\
tex Revenue per capita 					& `coeff8_tot_rev_avg_pc'`stars8_tot_rev_avg_pc' 										& `coeff1_tot_rev_avg_pc'`stars1_tot_rev_avg_pc'											& `coeff2_tot_rev_avg_pc'`stars2_tot_rev_avg_pc' 											& `coeff3_tot_rev_avg_pc'`stars3_tot_rev_avg_pc' 											& `coeff4_tot_rev_avg_pc'`stars4_tot_rev_avg_pc' 											& `coeff5_tot_rev_avg_pc'`stars5_tot_rev_avg_pc' 			\\
tex  														& (`se8_tot_rev_avg_pc') 																						& (`se1_tot_rev_avg_pc')																							& (`se2_tot_rev_avg_pc') 																							& (`se3_tot_rev_avg_pc') 																							& (`se4_tot_rev_avg_pc') 																							& (`se5_tot_rev_avg_pc') 						\\
tex Revenue, \% of GDP 					& `coeff8_tot_rev_avg_sgdp'`stars8_tot_rev_avg_sgdp' 								& `coeff1_tot_rev_avg_sgdp'`stars1_tot_rev_avg_sgdp' 									& `coeff2_tot_rev_avg_sgdp'`stars2_tot_rev_avg_sgdp' 									& `coeff3_tot_rev_avg_sgdp'`stars3_tot_rev_avg_sgdp' 									& `coeff4_tot_rev_avg_sgdp'`stars4_tot_rev_avg_sgdp' 									& `coeff5_tot_rev_avg_sgdp'`stars5_tot_rev_avg_sgdp' 			\\
tex  														& (`se8_tot_rev_avg_sgdp') 																					&  (`se1_tot_rev_avg_sgdp') 																					& (`se2_tot_rev_avg_sgdp') 																						& (`se3_tot_rev_avg_sgdp') 																						& (`se4_tot_rev_avg_sgdp') 																						& (`se5_tot_rev_avg_sgdp') 					 	\\
tex \midrule
tex \multicolumn{7}{c}{Allocation of resources: budget categories (\% of total expenditure)} \\
tex \midrule
tex Current Expenditure 				& `coeff8_current_exp_avg_share'`stars8_current_exp_avg_share' 			& `coeff1_current_exp_avg_share'`stars1_current_exp_avg_share' 				& `coeff2_current_exp_avg_share'`stars2_current_exp_avg_share' 				& `coeff3_current_exp_avg_share'`stars3_current_exp_avg_share' 				& `coeff4_current_exp_avg_share'`stars4_current_exp_avg_share' 				& `coeff5_current_exp_avg_share'`stars5_current_exp_avg_share' 			\\
tex  														& (`se8_current_exp_avg_share') 																		& (`se1_current_exp_avg_share')																				& (`se2_current_exp_avg_share') 																			& (`se3_current_exp_avg_share') 																			& (`se4_current_exp_avg_share') 																			& (`se5_current_exp_avg_share') 						\\
tex \multicolumn{7}{l}{of which:} \\
tex \hspace{0.20cm} Personnel 									& `coeff8_personnel_avg_share'`stars8_personnel_avg_share' 					& `coeff1_personnel_avg_share'`stars1_personnel_avg_share' 						& `coeff2_personnel_avg_share'`stars2_personnel_avg_share' 						& `coeff3_personnel_avg_share'`stars3_personnel_avg_share' 						& `coeff4_personnel_avg_share'`stars4_personnel_avg_share' 						& `coeff5_personnel_avg_share'`stars5_personnel_avg_share' 			\\
tex  														& (`se8_personnel_avg_share') 																			& (`se1_personnel_avg_share')																					& (`se2_personnel_avg_share') 																				& (`se3_personnel_avg_share') 																				& (`se4_personnel_avg_share') 																				& (`se5_personnel_avg_share') 						\\
tex Public Investment 					& `coeff8_investments_avg_share'`stars8_investments_avg_share' 			& `coeff1_investments_avg_share'`stars1_investments_avg_share' 				& `coeff2_investments_avg_share'`stars2_investments_avg_share' 				& `coeff3_investments_avg_share'`stars3_investments_avg_share' 				& `coeff4_investments_avg_share'`stars4_investments_avg_share' 				& `coeff5_investments_avg_share'`stars5_investments_avg_share' 			\\
tex  														& (`se8_investments_avg_share') 																		& (`se1_investments_avg_share')																				& (`se2_investments_avg_share') 																			& (`se3_investments_avg_share') 																			& (`se4_investments_avg_share') 																			& (`se5_investments_avg_share') 						\\
tex \midrule
tex \multicolumn{7}{c}{Allocation of resources: functional categories (\% of total expenditure)} \\
tex \midrule
tex Social Expenditures					& `coeff8_social_exp_avg_share'`stars8_social_exp_avg_share' 				& `coeff1_social_exp_avg_share'`stars1_social_exp_avg_share' 					& `coeff2_social_exp_avg_share'`stars2_social_exp_avg_share' 					& `coeff3_social_exp_avg_share'`stars3_social_exp_avg_share' 					& `coeff4_social_exp_avg_share'`stars4_social_exp_avg_share' 					& `coeff5_social_exp_avg_share'`stars5_social_exp_avg_share' 			\\
tex  														& (`se8_social_exp_avg_share') 																			& (`se1_social_exp_avg_share')																				& (`se2_social_exp_avg_share') 																				& (`se3_social_exp_avg_share') 																				& (`se4_social_exp_avg_share') 																				& (`se5_social_exp_avg_share') 						\\
tex \multicolumn{7}{l}{of which:} \\
tex \hspace{0.20cm} Health \& sanitation			& `coeff8_health_avg_share'`stars8_health_avg_share' 	& `coeff1_health_avg_share'`stars1_health_avg_share' 									& `coeff2_health_avg_share'`stars2_health_avg_share' 									& `coeff3_health_avg_share'`stars3_health_avg_share' 									& `coeff4_health_avg_share'`stars4_health_avg_share' 									& `coeff5_health_avg_share'`stars5_health_avg_share' 			\\
tex  														& (`se8_health_avg_share') 																					& (`se1_health_avg_share')																						& (`se2_health_avg_share') 																						& (`se3_health_avg_share') 																						& (`se4_health_avg_share') 																						& (`se5_health_avg_share') 						\\
tex  \hspace{0.20cm}Education \& culture 		& `coeff8_education_avg_share'`stars8_education_avg_share' 			& `coeff1_education_avg_share'`stars1_education_avg_share' 		& `coeff2_education_avg_share'`stars2_education_avg_share' 						& `coeff3_education_avg_share'`stars3_education_avg_share' 						& `coeff4_education_avg_share'`stars4_education_avg_share' 						& `coeff5_education_avg_share'`stars5_education_avg_share' 			\\
tex  														& (`se8_education_avg_share') 															& (`se1_education_avg_share')																									& (`se2_education_avg_share') 																				& (`se3_education_avg_share')					 																& (`se4_education_avg_share') 																				& (`se5_education_avg_share') 						\\
tex \hspace{0.20cm}Social welfare		& `coeff8_welfare_avg_share'`stars8_welfare_avg_share' 	& `coeff1_welfare_avg_share'`stars1_welfare_avg_share' 												& `coeff2_welfare_avg_share'`stars2_welfare_avg_share' 								& `coeff3_welfare_avg_share'`stars3_welfare_avg_share' 								& `coeff4_welfare_avg_share'`stars4_welfare_avg_share' 								& `coeff5_welfare_avg_share'`stars5_welfare_avg_share' 			\\
tex  														& (`se8_welfare_avg_share') 																& (`se1_welfare_avg_share')																										& (`se2_welfare_avg_share') 																					& (`se3_welfare_avg_share') 																					& (`se4_welfare_avg_share') 																					& (`se5_welfare_avg_share') 						\\
tex \midrule
tex \multicolumn{7}{l}{Other expenditures:} \\
tex \hspace{0.20cm} Housing 		& `coeff8_housing_avg_share'`stars8_housing_avg_share' 			& `coeff1_housing_avg_share'`stars1_housing_avg_share' 												& `coeff2_housing_avg_share'`stars2_housing_avg_share' 				& `coeff3_housing_avg_share'`stars3_housing_avg_share' 				& `coeff4_housing_avg_share'`stars4_housing_avg_share' 				& `coeff5_housing_avg_share'`stars5_housing_avg_share' 			\\
tex  														& (`se8_housing_avg_share') 																& (`se1_housing_avg_share')																										& (`se2_housing_avg_share') 																					& (`se3_housing_avg_share') 																					& (`se4_housing_avg_share') 																					& (`se5_housing_avg_share') 						\\
tex \hspace{0.20cm} Transportation		& `coeff8_transport_avg_share'`stars8_transport_avg_share' 			& `coeff1_transport_avg_share'`stars1_transport_avg_share' 					& `coeff2_transport_avg_share'`stars2_transport_avg_share' 						& `coeff3_transport_avg_share'`stars3_transport_avg_share' 						& `coeff4_transport_avg_share'`stars4_transport_avg_share' 						& `coeff5_transport_avg_share'`stars5_transport_avg_share' 			\\
tex  														& (`se8_transport_avg_share') 															& (`se1_transport_avg_share')																									& (`se2_transport_avg_share') 																				& (`se3_transport_avg_share') 																				& (`se4_transport_avg_share') 																				& (`se5_transport_avg_share') 						\\
tex \hspace{0.20cm} Other		& `coeff8_other_avg_share'`stars8_other_avg_share' 							& `coeff1_other_avg_share'`stars1_other_avg_share' 														& `coeff2_other_avg_share'`stars2_other_avg_share' 										& `coeff3_other_avg_share'`stars3_other_avg_share' 										& `coeff4_other_avg_share'`stars4_other_avg_share' 										& `coeff5_other_avg_share'`stars5_other_avg_share' 			\\
tex  														& (`se8_other_avg_share') 																	& (`se1_other_avg_share')																											& (`se2_other_avg_share') 																						& (`se3_other_avg_share') 																						& (`se4_other_avg_share') 																						& (`se5_other_avg_share') 						\\
tex \midrule
tex Social Expenditures per capita			& `coeff8_social_exp_avg_pc'`stars8_social_exp_avg_pc' 			& `coeff1_social_exp_avg_pc'`stars1_social_exp_avg_pc' 								& `coeff2_social_exp_avg_pc'`stars2_social_exp_avg_pc' 								& `coeff3_social_exp_avg_pc'`stars3_social_exp_avg_pc' 								& `coeff4_social_exp_avg_pc'`stars4_social_exp_avg_pc' 								& `coeff5_social_exp_avg_pc'`stars5_social_exp_avg_pc' 			\\
tex  																		& (`se8_social_exp_avg_pc') 																& (`se1_social_exp_avg_pc')																						& (`se2_social_exp_avg_pc') 																					& (`se3_social_exp_avg_pc') 																					& (`se4_social_exp_avg_pc') 																					& (`se5_social_exp_avg_pc') 						\\
tex \bottomrule
tex Observations (all) 						&  `n8_tot_exp_avg_pc'    																		& `n1_tot_exp_avg_pc'		 																									& `n2_tot_exp_avg_pc'  																								& `n3_tot_exp_avg_pc'  																								& `n4_tot_exp_avg_pc'  																								& `n5_tot_exp_avg_pc'  		\\
tex Observations (effective)			&  `effn8_tot_exp_avg_pc'																			& `effn1_tot_exp_avg_pc' 																									& `effn2_tot_exp_avg_pc'																							& `effn3_tot_exp_avg_pc'   																						& `effn4_tot_exp_avg_pc'																							& `effn5_tot_exp_avg_pc'   	\\
tex \bottomrule
tex \end{tabular}}

texdoc close
